package cn.yh.register.user.api;


import cn.yh.register.common.result.Result;
import cn.yh.register.common.util.AuthContextHolder;
import cn.yh.register.model.user.Patient;
import cn.yh.register.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "就诊人管理")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    PatientService patientService;

    @ApiOperation("查询所有就诊人")
    @GetMapping("/auth/findAll")
    public Result<List<Patient>> findAllPatient(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList = patientService.findByUserId(userId);
        return Result.ok(patientList);
    }

    @ApiOperation("获取就诊人详情")
    @GetMapping("/auth/get/{id}")
    public Result<Patient> getPatient(@PathVariable("id") Long patientId){
        Patient patient = patientService.getPatient(patientId);
        return Result.ok(patient);
    }

    @ApiOperation("添加就诊人")
    @PostMapping("/auth/save")
    public Result<?> add(@RequestBody Patient patient,HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    @ApiOperation("修改就诊人")
    @PostMapping("/auth/update")
    public Result<?> update(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }
    @ApiOperation("删除就诊人")
    @DeleteMapping("/auth/remove/{id}")
    public Result<?> delete(@PathVariable("id") Long patientId){
        patientService.removeById(patientId);
        return Result.ok();
    }

    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable Long id){
        return patientService.getPatient(id);
    }
}
